function graphsVec=plotDecompPerPage(x,cmat,sampleSt,stIn,nameSt)
% =========================================================================
% plotDecompPerPage(cmat,obsnames,shonames,title,offset);
%
% 1) *x*    [nobs nz] matrix of *demeaned* data.
%           Optional argument, if not passed then the observable will be
%           taken to be the sum of the cmat matrix
%
% 2) *cmat*      [nobs ns nx] matrix of counterfactuals
%
% 3) *obsnames*  [nobs 1] cell with name of the series
%
% 4) *shonames*  [nx   1] cell with name of the shocks
%
% 5) *sampleSS*  structure. Must contain
%           .sampleVec     [nobs 1] vector with sample dates in numeric form
%           .sampleCell    [nobs 1] cell   with sample dates in string  form
%           .scaling       (optional) [nobs 1] vector with the scalar with which
%                           to multiply
% 6) *tit*
% Plot is together with the series obtained as each shock fed, one at a
% time in the matrix CMAT [T nx] where nx is number of shocks.
% SHONAMES [nx 1] (cell)        Name of shocks
% SAMPLE                        Sample vector
% OFFSET   scalar               Determine widening of Y axis relative to Max/Min values
%
% Alejandro Justiniano January 21st 2010
% =========================================================================

%% 1. Check x can be decomposed or assign x
if isempty(x)
    x=squeeze( sum(cmat,3) );
else 
    maxdif=max(abs(x-sum(cmat,2))); 
    if maxdif > 1e-5 
        warning('Cannot recover Level with Counterfactual') 
    end 
end 
if nameSt.colNames~=size(cmat,2); 
    error('colNames does not match number of columns in CMAT'); 
end 
if nameSt.pageNames~=size(cmat,3); 
    error('pageNames does not match number of pages in CMAT'); 
end 
%% 3. Defaults%
stIn=plotAssignDefaults(stIn);
%% 2. Check sizes
t=length(x(:));
if size(cmat,1)~=t;
    error('Input and decomposition matrix do not match');
end
if length(sampleSt.sampleVec)~=size(x,1);
    error('Input and sample vector do not match');
end
nx=size(cmat,2);
if length(nameSt.shockNames)~=nx;
    error('Columns decomposition matrix and shonames do not match');
end
if ~isempty(stIn.axesFontSize); 
    set(0,'DefaultAxesFontSize',stIn.axesFontSize);
end 
%% 4. Assign
stIn.legend={'Data','Counterfactual'}; 
stIn.legendFontSize=12; 
stIn.xOffset=0.1; 
stIn.yOffset=0.1; 
stIn.flagBoxOff=1; 
stIn.lineWidths=[1.5 2.5];
if isfield(stIn,'NYears')==false; 
    stIn.NYears=5; 
end 
junk=assignXAxis(sampleSt.sampleVec,5); 
stIn.xtickValues=junk.xtickValues; 
stIn.xtickLabels=junk.xtickLabels;
%% Graph Settings 
NRows=4;
NCols=2;
ISubplots=0;
IGraphs  =0;
ICounter =0;
graphsVec =zeros( ceil(nx/(NRows*NCols)), 1);
NSubplots=nx;
for ii=1:NSubplots
    if ISubplots==0
        IGraphs=IGraphs+1;
        graphsVec(IGraphs)=figure;
    end
    ISubplots=ISubplots+1;
    ICounter =ICounter+1;
    subplot(NRows,NCols,ISubplots);
    if ISubplots==NRows*NCols || ii==NSubplots
        stIn.flagNoLegend=0; 
    else 
        stIn.flagNoLegend=1; 
    end 
    [plotHandle,legendHandle,figHandle]=plot3sampSub(sampleSt.sampleVec,[x cmat(:,ii)],nameSt.pageNames{ii},1,stIn); 
    if ISubplots==NRows*NCols || ii==NSubplots
        ISubplots=0;
        if length(graphsVec)==1
            supHand=suptitle(stIn.suptitle);
        else
            supHand=suptitle([char(stIn.suptitle),'   (',num2str(IGraphs),' of ',...
                num2str(ceil(nx/(NRows*NCols))),')']);
        end
        set(supHand,'FontSize',stIn.suptitleFontSize,'FontName',stIn.FontName,...
            'FontWeight','Bold');
        set(graphsVec(IGraphs),'PaperPosition',stIn.paperPosition)
    end
end
if ~isempty(stIn.axesFontSize); 
    set(0,'DefaultAxesFontSize','remove');
end 

